package com.csun.nusing.ui.familyactivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.BaseJsonBean;
import com.csun.nusing.presenter.CommonPresenter;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.ToastUtil;
import com.csun.zxing.CaptureActivity;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.Map;

/**
 * 手动设备添加界面
 * 冯志奇
 */
public class FamilyDeviceAddActivity extends BaseActivity<CommonPresenter> {

	private RadioGroup rg;
	private EditText codeet;
	private EditText infoet;
	private ImageView addiv;
	protected String devicetype = "3";
	private String devicecode;
	private String infostr;
	private String authorization;
	private TextView addtv;

	@Override
	public int getContentViewId() {
		return R.layout.activity_family_add_device;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		initView();
	}

	private void initView() {
		codeet = findViewById(R.id.devicecode_ed);
		infoet = findViewById(R.id.deviceinfo_ed);

		
		rg = findViewById(R.id.deviceresign_rg);
		addtv = findViewById(R.id.family_add_device_add_tv);
		addiv = findViewById(R.id.family_adddevice_ok_iv);
		// 默认选择第一个
		rg.check(R.id.device_rb1);
		rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.device_rb1:
					devicetype = "3";
					break;
				case R.id.device_rb2:
					devicetype = "2";
					break;
				case R.id.device_rb3:
					devicetype = "0";
					break;
				case R.id.device_rb4:
					devicetype = "1";
					break;
				default:
					break;
				}
			}
		});
		addtv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addDevice();
			}
		});
		addiv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				scanDevice();
			}
		});
	}

	/**
	 * 扫码添加
	 */
	private void scanDevice() {
		Intent intent = new Intent(this, CaptureActivity.class);
		startActivity(intent);
		finish();
	}

	/**
	 * 添加设备
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-17
	 */
	protected void addDevice() {
		devicecode = codeet.getText().toString().trim();
		infostr = infoet.getText().toString().trim();
		if (devicetype.equals("0")) {
			addFollow();
		} else if (devicetype.equals("1")) {
			addWatch();
		} else if (devicetype.equals("2")) {
			addBed();
		} else if (devicetype.equals("3")) {
			addAlert();
		}
	}

	private void addAlert() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("deviceNo", devicecode + "");
		param.put("deviceType", devicetype + "");
		param.put("deviceInfo", infostr + "");
		param.put("deviceMac", "");
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Gson gson = new Gson();
							BaseJsonBean baseJsonBean = gson.fromJson(result,
									BaseJsonBean.class);
							if (baseJsonBean.isStatus()) {
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
								info();
								finish();
							}else{
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
							}
						}
					}
				}).start(UrlBean.ADDALERTURL);
	}

	private void addBed() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("deviceNo", devicecode + "");
		param.put("deviceType", devicetype + "");
		param.put("deviceInfo", infostr + "");
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Gson gson = new Gson();
							BaseJsonBean baseJsonBean = gson.fromJson(result,
									BaseJsonBean.class);
							if (baseJsonBean.isStatus()) {
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
								info();
								finish();
							}else{
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
							}
						}
					}
				}).start(UrlBean.ADDSLEEPURL);
	}

	private void addWatch() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("deviceNo", devicecode + "");
		param.put("deviceType", devicetype + "");
		param.put("deviceInfo", infostr + "");
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Gson gson = new Gson();
							BaseJsonBean baseJsonBean = gson.fromJson(result,
									BaseJsonBean.class);
							if (baseJsonBean.isStatus()) {
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
								info();
								finish();
							}else{
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
							}
						}
					}
				}).start(UrlBean.ADDWATCHURL);
	}

	/**
	 * 添加随访仪
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-17
	 */
	private void addFollow() {
		Map<String, String> param = new HashMap<String, String>();
		param.put("deviceNo", devicecode + "");
		param.put("deviceType", devicetype + "");
		param.put("deviceInfo", infostr + "");
		param.put("androidMac", "");
		param.put("iosMac", "");
		OkHttpTask.newInstance(this).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Gson gson = new Gson();
							BaseJsonBean baseJsonBean = gson.fromJson(result,
									BaseJsonBean.class);
							if (baseJsonBean.isStatus()) {
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
								info();
								finish();
							}else{
								ToastUtil.showMessage(getApplicationContext(),
										baseJsonBean.getMessage());
							}
						}
					}
				}).start(UrlBean.ADDFOLLOWURL);
	}

	/**
	 * 跳转绑定界面
	 */
	private void info() {
		String resultString = devicetype + "," + devicecode;
		Intent intent = new Intent(this,FamilyDeviceInfoActivity.class);
		Bundle bundle = new Bundle();
		bundle.putString("url", resultString);
		intent.putExtras(bundle);
		startActivity(intent);
	}

	@Override
	public void initData() {
		super.initData();
		authorization = (String) SPUtils.get(this, "authorization", "");
	}

}
